946G - Almost Increasing Array - CodeForces Solution


data structures dp *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
#define ls segtree[now].pl
#define rs segtree[now].pr

const int inf = 1e9+10;
const int mxn = 2e5+10;
int n,arr[mxn],ldp[mxn],rdp[mxn];

struct node{
	int pl,pr,val;
	node(){
		pl = pr = val = 0;
	}
};

node segtree[mxn*32];
int ppp;

int newnode(){
	ppp++;
	segtree[ppp].pl = segtree[ppp].pr = segtree[ppp].val = 0;
	return ppp;
}

int setval(int now,int l,int r,int p,int v){
	if(!now)now = newnode();
	if(l == r){
		segtree[now].val = max(segtree[now].val,v);
		return now;
	}
	int mid = l+(r-l)/2;
	if(mid>=p)segtree[now].pl = setval(ls,l,mid,p,v);
	else segtree[now].pr = setval(rs,mid+1,r,p,v);
	segtree[now].val = max(segtree[ls].val,segtree[rs].val);
	return now;
}

int getval(int now,int l,int r,int s,int e){
	if(!now)return 0;
	if(l>=s&&e>=r)return segtree[now].val;
	int mid = l+(r-l)/2;
	if(mid>=e)return getval(ls,l,mid,s,e);
	else if(mid<s)return getval(rs,mid+1,r,s,e);
	else return max(getval(ls,l,mid,s,e),getval(rs,mid+1,r,s,e));
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	vector<pii> v;
	for(int i = 1;i<=n;i++){
		cin>>arr[i];
		arr[i] -= i;
	}
	ppp = 0;
	int rt = newnode();
	for(int i = 1;i<=n;i++){
		ldp[i] = getval(rt,-inf,inf,-inf,arr[i])+1;
		rt = setval(rt,-inf,inf,arr[i],ldp[i]);
	}
	ppp = 0;
	rt = newnode();
	for(int i = n;i>=1;i--){
		rdp[i] = getval(rt,-inf,inf,arr[i],inf)+1;
		rt = setval(rt,-inf,inf,arr[i],rdp[i]);
	}

	/*
	for(int i = 1;i<=n;i++)cout<<arr[i]<<' ';cout<<endl;
	for(int i = 1;i<=n;i++)cout<<ldp[i]<<' ';cout<<endl;
	for(int i = 1;i<=n;i++)cout<<rdp[i]<<' ';cout<<endl;
   */

	ppp = 0;
	rt = newnode();
	for(int i = 1;i<=n;i++){
		rdp[i] += getval(rt,-inf,inf,-inf,arr[i]+1);
		rt = setval(rt,-inf,inf,arr[i-1],ldp[i-1]);
	}
	cout<<max(0,n-*max_element(rdp+1,rdp+n+1)-1);
}


Comments

Submit
0 Comments
More Questions

1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits